package cn.compose.online.api;

import cn.compose.common.enums.ExecutorType;
import cn.compose.common.exception.runtime.ExecuteException;
import cn.compose.online.base.Response;
import cn.compose.online.base.ResponseBuilder;
import cn.compose.online.biz.CodeBiz;
import cn.compose.online.biz.UserTokenBiz;
import cn.compose.online.biz.TestBiz;
import cn.compose.online.constant.GroovyConstants;
import cn.compose.online.entity.Area;
import cn.compose.online.entity.UserToken;
import cn.compose.online.util.ParamsParseUtils;
import cn.compose.runtime.Executor;
import cn.compose.runtime.ExecutorContext;
import cn.compose.runtime.ExecutorFactory;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api")
@Slf4j
public class CodeController {
    @Resource
    private ExecutorFactory executorFactory;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private TestBiz testBiz;

    @Resource
    private UserTokenBiz userTokenBiz;

    @Resource
    private CodeBiz codeBiz;


    @PostMapping("/generateCode")
    public Response generateCode(@RequestParam("data") String data) {
        log.info(data);
        return codeBiz.generateCode(data);
    }

    @PostMapping("/saveCode")
    public Response saveCode(@RequestParam("historyId") String historyId) {
        return codeBiz.saveCode(historyId);
    }

    @PostMapping("/loginToken")
    public Response login(@RequestParam(value = "email") String email) {
        log.info(email);
        JSONObject result = JSONUtil.createObj();
        UserToken userToken = userTokenBiz.getEmailToken(email);
        if (userToken == null) {
            result.set("data", "");
            return ResponseBuilder.ok(result, "邮箱未授权！");
        }

        StpUtil.login(userToken.getUserId());
        StpUtil.getTokenSession().set(userToken.getUserId(), StpUtil.getTokenValue());
        result.set("token", StpUtil.getTokenValue());
        return ResponseBuilder.ok(result, "授权成功！");
    }

    @PostMapping("/getEmailToken")
    public Response getEmailToken(@RequestParam(value = "email") String email) {
        return ResponseBuilder.ok(userTokenBiz.getEmailToken(email), "成功！");
    }

    @PostMapping("/addEmailToken")
    public Response addEmailToken(@RequestParam(value = "email") String email) {
        userTokenBiz.addEmailToken(email);
        return ResponseBuilder.ok("", "新增成功！");
    }


    @RequestMapping("/test")
    public Object test(@RequestParam String code, @RequestParam String method) throws ExecuteException {
        Executor executor = executorFactory.getExecutor(ExecutorType.GROOVY);
        ExecutorContext executeContext = new ExecutorContext();
        executeContext.setCode(code);
        executeContext.setDebug(true);
        executeContext.setMethodName(method);
        Object execute = executor.execute(executeContext);

        return execute;
    }

    @GetMapping("/redisTest")
    public Object testRedis() {
        redisTemplate.opsForValue().set("redisTest", "test");
        List<Area> areaList = testBiz.listArea();
        System.err.println(JSON.toJSON(areaList));
        return redisTemplate.opsForValue().get("redisTest");
    }
}
